import React, { Component } from 'react'
import { Switch, Route, Redirect } from 'react-router-dom'
import { Frame } from './components'
import routes from './routes'

import { connect } from 'react-redux'
import { getNotificationsAction } from './actions/notifications'

const navRoutes = routes.filter(route => route.isNav === true)

@connect(
  null,
  { getNotificationsAction }
)
export default class App extends Component {
  componentDidMount() {
    this.props.getNotificationsAction()
  }
  render() {
    return (
      <Frame routes={navRoutes}>
        <Switch>
          {routes.map(route => {
            return (
              <Route
                exact
                key={route.path}
                path={route.path}
                render={props => {
                  if (route.hasAuth === false)
                    return <Redirect to="/admin/noauth" />
                  return <route.component {...props} />
                }}
              />
            )
          })}
          <Redirect to="/admin/dashboard" from="/admin" exact />
          <Redirect to="/404" />
        </Switch>
      </Frame>
    )
  }
}
